# 空元组
empty_tuple = ()
 
# 单元素元组（注意必须加逗号）
single_tuple = (42,)
 
# 多元素元组
multi_tuple = (1, 2, 3, "Python", True)
 
print(empty_tuple)  # 输出：()
print(single_tuple) # 输出：(42,)
print(multi_tuple)  # 输出：(1, 2, 3, 'Python', True)

mixed_tuple = (1, "Python", [2, 4, 6], {'key': 'value'}, (42,))
print(mixed_tuple)
# 输出：(1, 'Python', [2, 4, 6], {'key': 'value'}, (42,))


t = 1, 2, 3
print(t)  # 输出：(1, 2, 3)
print(type(t))  # 输出：<class 'tuple'>

t = (10, 20, 30, 40, 50)
 
# 正向索引
print(t[0])  # 输出：10
print(t[3])  # 输出：40
 
# 负向索引
print(t[-1])  # 输出：50
print(t[-2])  # 输出：40


t = (10, 20, 30, 40, 50)
 
# 获取子元组
print(t[1:4])  # 输出：(20, 30, 40)
print(t[:3])   # 输出：(10, 20, 30)
print(t[::2])  # 输出：(10, 30, 50) （步长为 2）
print(t[::-1]) # 输出：(50, 40, 30, 20, 10) （反转元组）


# t = (10, 20, 30)
 
# # 尝试修改元素
# t[0] = 100  # 报错：TypeError: 'tuple' object does not support item assignment

t = (1, 2, [3, 4])
 
# 修改元组中的列表
t[2][0] = 99
print(t)  # 输出：(1, 2, [99, 4])

t1 = (1, 2, 3)
t2 = (4, 5, 6)
 
# 合并
t3 = t1 + t2
print(t3)  # 输出：(1, 2, 3, 4, 5, 6)
 
# 重复
t4 = t1 * 2
print(t4)  # 输出：(1, 2, 3, 1, 2, 3)


t = (10, 20, 30)
 
print(20 in t)      # 输出：True
print(40 not in t)  # 输出：True

t = (1, 2, 3, 4, 5)
print(len(t))  # 输出：5


t = (10, 20, 30)
 
print(max(t))  # 输出：30
print(min(t))  # 输出：10
print(sum(t))  # 输出：60

t = (1, 2, 3)
 
# 解包
a, b, c = t
print(a, b, c)  # 输出：1 2 3

t = (1, 2, 3, 4, 5)
 
# 解包
a, *b, c = t
print(a)  # 输出：1
print(b)  # 输出：[2, 3, 4]
print(c)  # 输出：5

t = (10, 20, 30)
 
for item in t:
    print(item)
# 输出：
# 10
# 20
# 30


lst = [1, 2, 3]
t = tuple(lst)
print(t)  # 输出：(1, 2, 3)


t = (4, 5, 6)
lst = list(t)
print(lst)  # 输出：[4, 5, 6]


# 元组作为字典键
locations = {
    (40.7128, -74.0060): "New York",
    (34.0522, -118.2437): "Los Angeles"
}
print(locations[(40.7128, -74.0060)])  # 输出：New York


def get_coordinates():
    return (40.7128, -74.0060)

print(get_coordinates())  # 输出：(40.7128, -74.0060)


nested_tuple = ((1, 2, 3), (4, 5), (6, 7, 8))
 
# 访问嵌套元组的元素
print(nested_tuple[0])       # 输出：(1, 2, 3)
print(nested_tuple[0][1])    # 输出：2
print(nested_tuple[2][2])    # 输出：8


from collections import namedtuple
 
# 定义具名元组
Point = namedtuple('Point', ['x', 'y'])
 
# 实例化具名元组
p = Point(10, 20)
 
print(p.x)  # 输出：10
print(p.y)  # 输出：20
print(p)    # 输出Point(x=10, y=20) 

from collections import namedtuple

# 定义一个具名元组类型，名为 'Point'，字段为 'x' 和 'y'
Point = namedtuple('Point', ['x', 'y'])

# 实例化具名元组
p = Point(10, 20)

# 通过字段名访问值
print(p.x)  # 输出：10
print(p.y)  # 输出：20

# 通过索引访问值
print(p[0])  # 输出：10
print(p[1])  # 输出：20

import timeit
 
# 测试元组和列表的创建时间
print("元组创建时间：", timeit.timeit("(1, 2, 3, 4, 5)", number=1000000))
print("列表创建时间：", timeit.timeit("[1, 2, 3, 4, 5]", number=1000000))

import sys
 
lst = [1, 2, 3, 4, 5]
tup = (1, 2, 3, 4, 5)
 
print("列表的内存占用：", sys.getsizeof(lst))  # 输出：内存大小（单位：字节）
print("元组的内存占用：", sys.getsizeof(tup))  # 输出：内存大小（单位：字节）

t = (1, 2, 3)
lst = list(t)
lst[0] = 99
t = tuple(lst)
print(t)  # 输出：(99, 2, 3)

t = (1, 2, 3)
t = (99,) + t[1:]
print(t)  # 输出：(99, 2, 3)

t = (3, 1, 4, 2)
 
# 转换为列表排序
sorted_t = tuple(sorted(t))
print(sorted_t)  # 输出：(1, 2, 3, 4)


t = (1, 2, 3, 2, 4, 2)
 
# 统计 2 出现的次数
print(t.count(2))  # 输出：3
 
# 查找 4 的索引
print(t.index(4))  # 输出：4